<?php

abstract class GaAPI_Clients_Abstract {

    protected $_domain = 'adwords.google.com';
    protected $_sandbox = 'sandbox.google.com';
    protected $_namespace = false;
    protected $_service;

    /**
     * @param GaAPI_Methods_Abstract
     */
    public function setMethod($method = null) {
        $this->_method = $method;
    }


    public function getMethod() {
        return $this->_method;
    }


    public function getApi() {
        return $this->getMethod()->getApi();
    }

    public function getNamespace() {
        return 'https://' . $this->_domain . $this->_namespace;
    }

    public function getRequestURL() {
        $domain    = ($this->getApi()->isSandbox()) ? $this->_sandbox : $this->_domain;
        $namespace = $this->_namespace;

        return 'https://' . $domain . $namespace;
    }


    public function checkLoginParams() {
        return $this->getMethod()->getApi()->checkLoginParams();
    }


    public function getDefaultHeaders() {
        $headers = '';
        $headers .= '<email>' . $this->getApi()->email . '</email>';
        $headers .= '<password>' . $this->getApi()->password . '</password>';
        if ($this->getApi()->client_id) {
            $headers .= '<clientCustomerId>' . $this->getApi()->client_id . '</clientCustomerId>';
        } elseif ($this->getApi()->client_email) {
            $headers .= '<clientEmail>' . $this->getApi()->client_email . '</clientEmail>';
        }

        $headers .= '<useragent>AgileCats AdWords API</useragent>';
        $headers .= '<developerToken>' .  ((!$this->getApi()->isSandbox()) ? $this->getApi()->developer_token : ($this->getApi()->email . "++USD")) . '</developerToken>';
        $headers .= '<applicationToken>' . $this->getApi()->application_token . '</applicationToken>';

        return $headers;
    }


    public function getHeaders() {
        $this->checkLoginParams();
        return $this->getDefaultHeaders();
    }

    protected function throwError($error_msg) {
        $this->_method->throwError($error_msg);
    }


}